package com.travel.travelServer.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.User;
import com.travel.travelServer.entity.vo.UserVO;
import com.travel.travelServer.service.UserService;
import com.travel.travelServer.utils.BaseController;
import com.travel.travelServer.utils.ResponseData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2023-10-24 16:46:08
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
//    @PageableDefault(value = 50, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable,
    @GetMapping
    public ResponseData selectAll(
            @RequestParam Map<String, Object> conditions
    ) {
        Page<UserVO> page = new Page<>(Integer.valueOf(conditions.get("page").toString()),Integer.valueOf(conditions.get("size").toString()));
        return ResponseData.success(userService.pageByUser(page, conditions));
    }

    @GetMapping("/{id}")
    public ResponseData selectOne(@PathVariable("id") Long id) {
        return ResponseData.success(userService.getById(id));
    }

    @GetMapping("/getByCurrentUser")
    public ResponseData getByCurrentUser() {
        return ResponseData.success(userService.getByCurrentUser());
    }

    @PostMapping
    public ResponseData insert(@RequestBody User user) {
        return ResponseData.success(userService.saveUser(user));
    }

    @PutMapping
    public ResponseData update(@RequestBody User user) {
        return ResponseData.success(userService.updateUserById(user));
    }

    @DeleteMapping("/{id}")
    public ResponseData deleteById(@PathVariable("id") Long id) {
        return ResponseData.success(userService.removeById(id));
    }

    @PostMapping("/batchDeleteUser")
    public ResponseData delete(@RequestBody Long[] idList) {
        return ResponseData.success(userService.removeByIds(Arrays.asList(idList)));
    }

    @PostMapping("/setRoles/{id}")
    public ResponseData setRoles(@RequestBody Long[] idList, @PathVariable("id") Long id) {
        return ResponseData.success(userService.setRoles(Arrays.asList(idList), id));
    }

    @GetMapping("/getLoginUserVOById")
    public ResponseData getLoginUserVOById() {
        return ResponseData.success(userService.getLoginUserVOById());
    }

    @PostMapping("/updatePassword")
    public ResponseData updatePassword(@RequestBody User user) {
        return ResponseData.success(userService.updatePassword(user));
    }
}
